package hu.bme.web;
import hu.bme.domain.Table;
import hu.bme.service.GameService;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/game")
@Controller
public class GameController 
{
	@Autowired
	GameService gameService;
	
	
    @RequestMapping(produces = "text/html")
    public String show(Model uiModel) 
    {        
    	uiModel.addAttribute("player", gameService.generatePlayerToken());
    	uiModel.addAttribute("table", gameService.getTable());
    	
        return "test";
    }
    
	@RequestMapping(value = "/table", method = RequestMethod.GET, produces="application/json")
	public @ResponseBody ResponseEntity<Table> jsonTest() 
	{	     
		Table table = gameService.getTable();
		if(gameService.startGameIfTwoPlayerConnected())
		{
			return new ResponseEntity<Table>(table, HttpStatus.OK);
		}
		return new ResponseEntity<Table>(table, HttpStatus.NOT_ACCEPTABLE);		
	}
	
	@RequestMapping(value = "/move", method = RequestMethod.POST)
	public void move(HttpServletResponse res, @RequestParam(value="id") String id, @RequestParam(value="playerToken") String playerToken) 
	{
		gameService.move(playerToken,id);
	}
	
	
	@RequestMapping(value = "/reset")
	public String resetPlayerCounter()
	{
		gameService.reset();
		return "game";
	}
	
	
}
